import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { NzMessageService, NzFormatEmitEvent, NzTreeNode, NzTreeComponent } from 'ng-zorro-antd';
import { Observable, of } from 'rxjs';

import { PopupRes } from '@shared/popup/popup.interface';
import { I18NService } from '@core/i18n/i18n.service';
import { RoleService } from '../service/role.service';
import { MenuService } from '../../menu/services/menu.service';

@Component({
  selector: 'permission-setting',
  templateUrl: './permission-module.html'
})

export class PermissionSetComponent implements OnInit, PopupRes {

  _id: number;

  searchValue: string;

  nodes = [];

  @ViewChild('nzTree')
  nzTree: NzTreeComponent;

  @Input()
  set id(val: number) {
    this._id = val;
  }

  isCheck = true;

  isSpinning = false;

  page = {
    pageNo: 1,
    pageSize: 100
  };

  constructor(
    private message: NzMessageService,
    private i18nService: I18NService,
    private roleService: RoleService,
    private menuService: MenuService
  ) { }

  ngOnInit(): void {
    if (this._id !== undefined) {
      this.getMenuByPage();
    }
  }

  mouseAction(name: string, event: NzFormatEmitEvent): void { }

  submit(): Observable<any> {
    return new Observable((sub) => {
      const mId = getRoleMenuId(this.nzTree.getCheckedNodeList());
      this.roleService.saveRoleMenuFunction(this._id, mId).subscribe(res => {
          this.message.success(this.i18nService.translate('modify.return.edit_success'));
          sub.next();
        }, err => {
          this.message.error(this.i18nService.translate('modify.return.eidt_error'));
          sub.error();
      });
    });
  }

  cancel(): Observable<any> {
    return of('cancel');
  }

  getMenuByPage(reset: boolean = false): void {
    if (reset) {
      this.page.pageNo = 1;
    }
    this.isSpinning = true;
    this.menuService.getMenuByPageWithRole(this.page.pageNo, this.page.pageSize, this._id)
      .subscribe(res => {
        const data = res.records;
        const node = getChildren(data);
        this.nodes = node.map((w) => {
          return new NzTreeNode(w);
        });
        this.isSpinning = false;
      }, err => {
        this.isSpinning = false;
      });
  }

}

function getChildren(arr: Array<any>) {
  if (arr === undefined) {
    return [];
  }
  return arr.map((w, i) => {
    return {
      key: w.id,
      title: w.name,
      children: getChildren(w.children),
      checked: w.checked && !(w.children !== undefined && w.children.length > 0),
      isLeaf: !(w.children !== undefined && w.children.length > 0),
      selectable: false
    };
  });
}

function getRoleMenuId(arr: Array<any>, a: Array<any> = []) {
  if (arr === undefined) {
    return a;
  }
  const set = new Set<any>(a);
  console.log(set);
  arr.filter((f) => f.isChecked).forEach((w) => {
    set.add(w.key);
    w.children.forEach((c) => {
      set.add(c.key);
    });
    if (w.parentNode !== null) {
      set.add(w.parentNode.key);
    }
    a = Array.from(set);
    getRoleMenuId(w.children, a);
  });
  return a;
}
